package gui;

import gui.control.TDControlFrame;

import javax.swing.JFrame;
import javax.swing.JRootPane;

import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.WindowEvent;

/**
 * 
 * @author Alex
 *
 * Fenetre principale
 */

/*
 * TODO
 * 1. Synchroniser le focus de tte les fenetres
 * 2. Synchroniser aussi la fermeture des fenetres
 */
public class TDFrame extends JFrame  implements TDprop{

	TDMenuBar tdMenu;
	
	TDControlFrame cframe;
	TDGameFrame gframe;
	
	//java.util.Vector<E> allgame;
	private Point position = new Point();
	public double propx; // entre 0 ... 1
	public double propy; // entre 0 ... 1
	
	public double realWidth ;
	public double realHeight;
	public int border;
	
	public TDFrame(double px, double py)
	{
		super("ThreeDGames");
		tdMenu = new TDMenuBar();
		propx = px;
		propy = py;
		
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		
		this.setUndecorated(true);
		this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
		
		this.setJMenuBar(tdMenu);
		this.setSize((int)(d.width*propx), 55);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		this.setVisible(true);
		
		border = 5;
		realWidth = d.width*propx;
		realHeight= d.height*propy;
		cframe = new TDControlFrame(this);
		gframe = new TDGameFrame(this);
		this.center(d);	
		position = this.getLocation();
		this.enableEvents(AWTEvent.FOCUS_EVENT_MASK);
	//	this.getRootPane().add(cframe);
	//	this.getRootPane().add(gframe);
	}
	
	/*
	 * Positione les trois fenetres sur l'ecran
	 */
	public void center(Dimension d)
	{
		double xp = d.width*(1- propx)/2;
		double yp = d.height*(1- propy)/2;
		setLocation((int)xp, (int)yp);
		cframe.center(d);
		gframe.center(d);
		//TODO
	}
	
	@SuppressWarnings("deprecation")
	protected void processEvent(AWTEvent e)	
	{
		if(e.getID() == ComponentEvent.COMPONENT_MOVED)
		{
			Point translation = new Point(getLocation().x - position.x, getLocation().y - position.y);
			
			cframe.setLocation(cframe.getLocation().x + translation.x, cframe.getLocation().y + translation.y);
			gframe.setLocation(gframe.getLocation().x + translation.x, gframe.getLocation().y + translation.y);
			position = getLocation();
		}
		else if(e.getID() == WindowEvent.WINDOW_DEICONIFIED )//||
				//e.getID() == FocusEvent.FOCUS_GAINED)
			this.showAll();
		
		else if(e.getID() == WindowEvent.WINDOW_ICONIFIED)
		{}	
		
		super.processEvent(e);
	}
	
	public void showAll()
	{
		cframe.show();
		gframe.show();
		show();
	}
	public static void main(String[] args)
	{
		TDFrame jeux = new TDFrame(.7, .7);
	}	
}
